package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.ZnxController;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.Purta;
import com.example.entity.yifei.Purtb;
import com.example.entity.yifei.Purtl;
import com.example.entity.yifei.Purtm;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.PurtlMapper;
import com.example.mapper.PurtmMapper;
import com.example.service.IActFlowCustomService;
import com.example.service.PurtlService;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurtlServiceImpl extends ServiceImpl<PurtlMapper, Purtl> implements PurtlService, IActFlowCustomService {

    @Resource
    private PurtlMapper purtlMapper;
    @Resource
    private PurtmMapper purtmMapper;

    @Override
    public Boolean tc30IsN(String tc001, String tc002) {
        LambdaQueryWrapper<Purtl> queryWrapper= new LambdaQueryWrapper<Purtl>();
        queryWrapper.eq(Purtl::getTL001,tc001).eq(Purtl::getTL002,tc002);
        Purtl purtl = purtlMapper.selectOne(queryWrapper);
        if (purtl!=null&&purtl.getTL012().equals("0")){
            return true;
        }else {
            throw new BaseException(tc001+"-"+tc002+"该核价单不是未审核核价单");
        }
    }
    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","核价单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Override
    public void startRunTask(String id) {

    }
    @Autowired
    private ZnxController znxController;
    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("核价单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Purtl purtl = purtlMapper.selectOne(new LambdaQueryWrapper<Purtl>()
                .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
        if(purtl == null) {
            throw new CustomException("500", "核价单不存在");
        }
        if(state) {  // 审核通过
            purtl.setTL006("3");
            purtlMapper.update(purtl, new LambdaQueryWrapper<Purtl>()
                    .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
            checkPurtl(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的核价单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            purtl.setTL006("2");
            purtlMapper.update(purtl, new LambdaQueryWrapper<Purtl>()
                    .eq(Purtl::getTL001, strings[0]).eq(Purtl::getTL002, strings[1]));
        }
    }
    public void checkPurtl(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Purtl purtl = purtlMapper.selectOne(new LambdaQueryWrapper<Purtl>().eq(Purtl::getTL001, TA001).eq(Purtl::getTL002, TA002));
        if(purtl == null) {
            throw new CustomException("500", "单据不存在!");
        }
        purtl.setTL006("Y");
        purtlMapper.update(purtl, new LambdaQueryWrapper<Purtl>().eq(Purtl::getTL001, TA001).eq(Purtl::getTL002, TA002));
        Purtm purtm = new Purtm();
        purtm.setTm011("Y");
        purtmMapper.update(purtm, new LambdaQueryWrapper<Purtm>().eq(Purtm::getTm001, TA001).eq(Purtm::getTm002, TA002));
    }
    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }
}
